package com.zmm.weblog.admin.service.impl;

import com.zmm.weblog.admin.model.vo.file.UploadFileVo;
import com.zmm.weblog.admin.service.AdminFileService;
import com.zmm.weblog.admin.utils.MinioUtil;
import com.zmm.weblog.common.enums.ResponseCodeEnum;
import com.zmm.weblog.common.exception.BizException;
import com.zmm.weblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author:福禄寿
 * @Date:2024/5/9 下午2:21
 * @Des:weblog-springboot
 **/
@Service
@Slf4j
public class AdminFileServiceImpl implements AdminFileService {

    @Autowired
    private MinioUtil minioUtil;

    @Override
    public Response uploadFile(MultipartFile file) {

        try {
            String url = minioUtil.uploadFile(file);
            return Response.success(UploadFileVo.builder().url(url).build());
        } catch (Exception e) {
            log.warn("文件上传失败");
            throw new BizException(ResponseCodeEnum.FILE_UPLOAD_FAILED);
        }
    }
}
